package com.company.project.controller.wx.demo;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <pre>
 *  注意：此contorller 实现WxMpMenuService接口，仅是为了演示如何调用所有菜单相关操作接口，
 *      实际项目中无需这样，根据自己需要添加对应接口即可
 * </pre>
 *
 * @author Binary Wang(https://github.com/binarywang)
 */
@RestController("myWxMenuController")
@RequestMapping("/wxdemo/mymenu")
public class WxMenuController  {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMpService wxService;


    @GetMapping("/create")
    public String menuCreateSample() throws WxErrorException {
        WxMenu menu = new WxMenu();
        WxMenuButton button1 = new WxMenuButton();
        button1.setType(WxConsts.BUTTON_CLICK);
        button1.setName("今日歌曲");
        button1.setKey("V1001_TODAY_MUSIC");

//        WxMenuButton button2 = new WxMenuButton();
//        button2.setType(WxConsts.BUTTON_MINIPROGRAM);
//        button2.setName("小程序");
//        button2.setAppId("wx286b93c14bbf93aa");
//        button2.setPagePath("pages/lunar/index.html");
//        button2.setUrl("http://mp.weixin.qq.com");

        WxMenuButton button3 = new WxMenuButton();
        button3.setName("菜单");

        menu.getButtons().add(button1);
//        menu.getButtons().add(button2);
        menu.getButtons().add(button3);

        WxMenuButton button31 = new WxMenuButton();
        button31.setType(WxConsts.BUTTON_VIEW);
        button31.setName("搜索");
        button31.setUrl("http://www.soso.com/");

        WxMenuButton button32 = new WxMenuButton();
        button32.setType(WxConsts.BUTTON_VIEW);
        button32.setName("视频");
        button32.setUrl("http://v.qq.com/");

        WxMenuButton button33 = new WxMenuButton();
        button33.setType(WxConsts.BUTTON_CLICK);
        button33.setName("赞一下我们");
        button33.setKey("V1001_GOOD");

        WxMenuButton button34 = new WxMenuButton();
        button34.setType(WxConsts.BUTTON_SCANCODE_PUSH);
        button34.setName("扫一扫");
        button34.setKey("rselfmenu_0_0");
        button34.setSubButtons(new ArrayList<WxMenuButton>());

        button3.getSubButtons().add(button31);
        button3.getSubButtons().add(button32);
        button3.getSubButtons().add(button33);
        button3.getSubButtons().add(button34);

        return this.wxService.getMenuService().menuCreate(menu);
    }

    @GetMapping("/createConditionalMenu")
    public void CreateConditionalMenu() throws WxErrorException {
        String json = "{\n" +
                " 	\"button\":[\n" +
                " 	{	\n" +
                "    	\"type\":\"click\",\n" +
                "    	\"name\":\"今日歌曲\",\n" +
                "     	\"key\":\"V1001_TODAY_MUSIC\" \n" +
                "	},\n" +
                "	{ \n" +
                "		\"name\":\"菜单\",\n" +
                "		\"sub_button\":[\n" +
                "		{	\n" +
                "			\"type\":\"view\",\n" +
                "			\"name\":\"搜索\",\n" +
                "			\"url\":\"http://www.soso.com/\"\n" +
                "		},\n" +
                "		{\n" +
                "			\"type\":\"view\",\n" +
                "			\"name\":\"视频\",\n" +
                "			\"url\":\"http://v.qq.com/\"\n" +
                "		},\n" +
                "		{\n" +
                "			\"type\":\"click\",\n" +
                "			\"name\":\"赞一下我们\",\n" +
                "			\"key\":\"V1001_GOOD\"\n" +
                "		}]\n" +
                " }],\n" +
                "\"matchrule\":{\n" +
                "  \"tag_id\":\"2\",\n" +
                "  \"sex\":\"1\",\n" +
                "  \"country\":\"中国\",\n" +
                "  \"province\":\"广东\",\n" +
                "  \"city\":\"广州\",\n" +
                "  \"client_platform_type\":\"2\",\n" +
                "  \"language\":\"zh_CN\"\n" +
                "  }\n" +
                "}";

        String menuId = this.wxService.getMenuService().menuCreate(json);
        logger.debug("menuId:" + menuId);
    }

    /**
     * <pre>
     * 测试个性化菜单匹配结果
     * 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
     * </pre>
     *
     * @param userid 可以是粉丝的OpenID，也可以是粉丝的微信号。
     */
    @GetMapping("/menuTryMatch/{userid}")
    public WxMenu menuTryMatch(@PathVariable String userid) throws WxErrorException {
        return this.wxService.getMenuService().menuTryMatch(userid);
    }



    /**
     * <pre>
     * 自定义菜单查询接口
     * 详情请见： https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014&token=&lang=zh_CN
     * </pre>
     */
    @GetMapping("/get")
    public WxMpMenu menuGet() throws WxErrorException {
        return this.wxService.getMenuService().menuGet();
    }
    /**
     * <pre>
     * 获取自定义菜单配置接口
     * https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN
     * </pre>
     */
    @GetMapping("/getSelfMenuInfo")
    public WxMpGetSelfMenuInfoResult getSelfMenuInfo() throws WxErrorException {
        return this.wxService.getMenuService().getSelfMenuInfo();
    }


    /**
     * <pre>
     * 自定义菜单删除接口
     * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015&token=&lang=zh_CN
     * </pre>
     */
    @GetMapping("/delete")
    public void menuDelete() throws WxErrorException {
        this.wxService.getMenuService().menuDelete();
    }

    /**
     * <pre>
     * 删除个性化菜单接口
     * 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
     * </pre>
     *
     * @param menuId 个性化菜单的menuid
     */
    @GetMapping("/delete/{menuId}")
    public void menuDelete(@PathVariable String menuId) throws WxErrorException {
        this.wxService.getMenuService().menuDelete(menuId);
    }



}
